State machine with enum

상태들과 상태 간의 전이를 enum을 이용해서 정의
enum class State{
off_hook,
connecting,
connected,
on_hold,
on_hook
};
enum class Trigger{
call_dialed,
hung_up,
call_connected,
placed_on_hold,
taken_off_hold,
left_message,
stop_using_phone
};
상태 머시에서 상태 간 전이가 어떤 규칙을 이루어져야 하는지에 대한 정보를 저장
map<State, vector<pair<Trigger, State>>> rules;
rules[State::off_hook]={
{Trigger::call_dialed, State::connecting},
{Trigger::stop_using_phone, State::on_hook}
};
rules[State::connecting]={
{Trigger::hung_up, State::off_hook},
{Trigger::call_connected, State::connected}
};
// ...
//
State currentState{State::off_hook}, exitState{State::on_hook};
//
while(true){
cout<<"The phone is currently "<<currentState<<endl;
select_trigger:
cout<<"Select a trigger:"<<'\n';
int i=0;
for(auto item: rules[currentState]){
cout<<i++<". "<<item.first<<'\n';
}
int input;
cin>>input;
if(input<0 || (input+1)>rules[currentState].size()){
cout<<"Incorrect option. Please try again."<<'\n';
goto select_trigger;
}
currentState=rules[currentState][input].second;
if(currentState==exitState) break;
}